// Created by WXX on 2021/9/6 9:33
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<int> sortedSquares(vector<int> &nums) {
        int n = nums.size();

        vector<int> res;
        int i = 0, j = n - 1;
        while (i <= j) {
            int t = 0;
            if (abs(nums[i]) > abs(nums[j])) t = nums[i++];
            else t = nums[j--];

            res.push_back(t * t);
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

void OutputBasicArray1D(vector<int> nums) {
    cout << "[";
    for (int i = 0; i < nums.size(); i++) {
        cout << nums[i];
        if (i != nums.size() - 1) cout << ", ";
    }
    cout << "]" << endl;
}

int main() {

    vector<int> nums = {-4, -1, 0, 3, 10};
    OutputBasicArray1D(Solution().sortedSquares(nums));  // [0, 1, 9, 16, 100]

    return 0;
}
